package com.gdsdxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gdsdxy.dto.CommentDto;
import com.gdsdxy.dto.ResultVo;
import com.gdsdxy.dto.UserDTO;
import com.gdsdxy.entity.Blog;
import com.gdsdxy.entity.Comments;
import com.gdsdxy.mapper.BlogMapper;
import com.gdsdxy.mapper.CommentsMapper;
import com.gdsdxy.service.IBlogService;
import com.gdsdxy.service.ICommentsService;
import com.gdsdxy.service.IUserService;
import com.gdsdxy.utils.UserHolder;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class CommentsServiceImpl extends ServiceImpl<CommentsMapper, Comments> implements ICommentsService {
    @Autowired
    CommentsMapper commentsMapper;

    @Autowired
    IUserService iUserService;

    @Autowired
    BlogMapper blogMapper;
    @Autowired
    IBlogService iBlogService;

    @Override
    public ResultVo listComments(Long id){
        //1.根据博客id 查询 评论列表 (comments表)
        //2.根据作者的id 查询作者的信息
        //3.判断是否有子评论(parent_id)
        Comments comments = commentsMapper.selectById(id);
        return ResultVo.ok(comments);
    }

    @Override
    public ResultVo createcomment(CommentDto commentDto){
        // 拿到评论信息
        // 博文id  用户id 父评论id
        // 添加评论
        UserDTO userDTO = UserHolder.getUser();
        Long uid = userDTO.getId();
        Long bid1 = commentDto.getBid();
        Comments comments=new Comments();
        comments.setUid(uid);
        if(bid1.equals(null)||bid1==null){
            comments.setFcid(commentDto.getFcid());
        }else{
            Blog blog = blogMapper.selectById(bid1);
            if (blog==null){
                return ResultVo.fail("博文不存在");
            }else{
                comments.setBid(bid1);
//                Blog blog1 = blogMapper.selectById(bid1);
                Long uid1 = blog.getUid();
                if (uid.equals(uid1)){
                    comments.setIsAuthor(1);
                }else{
                    comments.setIsAuthor(0);
                }
            }
        }
        if(commentDto.getContent()==null||commentDto.getContent().equals(null)){
            return ResultVo.fail("请输入内容");
        }else{
            comments.setContent(commentDto.getContent());
        }
        commentsMapper.insert(comments);
        return ResultVo.ok(null,"评论成功");
    }

    @Override
    public ResultVo removeComment(Long id) {
        Comments comment = commentsMapper.selectById(id);
        if (comment == null){
            return  ResultVo.fail("没有评论！");
        }
        //删除评论
        boolean result = delComment(id);
        return result ? ResultVo.ok(null,"删除成功"):ResultVo.fail("删除失败");
    }
    // 删除评论
    private boolean delComment(Long id){
        Comments byId = getById(id);
        if (byId == null){
            return false;
        }
        List<Comments> list = query().eq("fid", id).list();
        if (!list.isEmpty()){
            for (Comments comments : list) {
                Long commentsid = comments.getId();
                boolean result= delComment(commentsid);
                if (!result){
                    return false;
                }
            }
        }
        return removeById(id);
    }

    @Override
    public ResultVo queryComments(Long id) {
        // 根据id查询评论
        Comments comments = getById(id);
        return comments != null ? ResultVo.ok(comments) : ResultVo.fail("评论不存在");
    }

    @Override
    public ResultVo updateComments(Comments comments) {
        // 获取评论id
        Long id = comments.getId();
        // 查询评论
        Comments before = getById(id);
        //判断评论是否存在
        if (before ==null) {
            return ResultVo.fail("评论不存在");
        }
        //修改评论
        Integer status = comments.getStatus();
        if (status == null) {
            return ResultVo.fail();
        }
        boolean result = update().set("status", status).eq("id", id).update();
        return result ? ResultVo.ok("修改成功") : ResultVo.fail("修改失败");
    }


}
